Ext.onReady(function() {
    Ext.form.Field.prototype.msgTarget = 'qtip';
    Ext.QuickTips.init();
    //元数据的store
    var store2 = new Ext.data.Store({
        storeId: 'store2',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=refuel'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                }, {
                    name: 'value'
                }
            ]
        })
    });

    //获取车辆列表
    var carList_store = new Ext.data.Store({
        storeId: 'store3',
        proxy: new Ext.data.HttpProxy({
            url: 'Get_Car_List.action'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                }, {
                    name: 'value'
                }
            ]
        })
    });

    //获取驾驶员列表
    var driverList_store = new Ext.data.Store({
        storeId: 'store4',
        proxy: new Ext.data.HttpProxy({
            url: 'Get_Data_List.action?method=driver'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                }, {
                    name: 'value'
                }
            ]
        })
    });

    //获取加油卡列表
    var cardList_store = new Ext.data.Store({
        storeId: 'store4',
        proxy: new Ext.data.HttpProxy({
            url: 'Get_Data_List.action?method=card'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                }, {
                    name: 'value'
                }
            ]
        })
    });

    var cardOrArea_store = new Ext.data.Store({
        storeId: 'store3',
        proxy: new Ext.data.HttpProxy({
            url: 'Get_Card_List.action'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                }, {
                    name: 'value'
                }
            ]
        })
    });


    var toolbar = new Ext.Toolbar({
        id: 'toolbar',
        stateful: true,
        items: ['-', {
                id: 'add-p',
                text: '新增',
                handler: function() {
                    card_win.show();
                }
            }, '-', {
                id: 'edit-btn',
                text: '编辑',
                handler: function() {
                    edit();
                }
            }, '-', {
                id: 'del-btn',
                text: '删除',
                handler: function() {
                    Ext.MessageBox.show({
                        title: '提示框',
                        msg: ' 确定删除该记录吗？注意，数据删除后将无法恢复！ ',
                        buttons: {
                            'yes': '确定',
                            'no': '取消'
                        },
                        fn: showResult,
                        icon: Ext.MessageBox.QUESTION
                    });

                    function showResult(btn) {
                        if (btn == 'yes') {
                            del();
                        }
                    }

                }
            }, '-', {
                text: '导出Excel',
                handler: function() {
                    window.open('ExportXls_RefuelCard.action?car=' + Ext.ComponentMgr.get("car_form").getValue() + '&card=' + Ext.ComponentMgr.get("card_or_area_form").getValue());
                }
            }, '-']
    });

    var form = new Ext.FormPanel({
        labelAlign: 'form',
        frame: true,
        bodyStyle: 'padding:5px 5px 0',
        width: '100%',
        items: [{
                layout: 'column',
                items: [{
                        columnWidth: .3,
                        layout: 'form',
                        items: [new Ext.form.ComboBox({
                                fieldLabel: '车牌号码',
                                name: 'car_form',
                                id: 'car_form',
                                displayField: 'name',
                                valueField: 'name',
                                emptyText: '车牌号码',
                                anchor: '95%',
                                typeAhead: true,
                                queryParam: 'name',
                                autoSelect: false,
                                        triggerAction : 'all',
                                        store: carList_store
                            })]
                    }, {
                        columnWidth: .3,
                        layout: 'form',
                        items: [new Ext.form.ComboBox({
                                xtype: 'textfield',
                                fieldLabel: '油卡编号',
                                emptyText: '油卡编号',
                                name: 'card_or_area_form',
                                displayField: 'name',
                                valueField: 'name',
                                store: cardOrArea_store,
                                id: 'card_or_area_form',
                                triggerAction: 'all',
                                anchor: '95%'
                            })]
                    }, {
                        columnWidth: .1,
                        layout: 'form',
                        items: [{
                                xtype: "button",
                                id: 'query_tbar',
                                text: "查  询",
                                width: 60,
                                handler: function() {
                                    store.setBaseParam("car", Ext.ComponentMgr.get("car_form").getValue());
                                    store.setBaseParam("card", Ext.ComponentMgr.get("card_or_area_form").getValue());
                                    store.load();
                                }
                            }]
                    }]
            }]
    });

    jReader1 = new Ext.data.JsonReader({
        root: 'data',
        totalProperty: 'totalCount',
        fields: [{
                name: 'id'
            }, {
                name: 'car_code'
            }, {
                name: 'type'
            }, {
                name: 'money'
            }, {
                name: 'quantity'
            }, {
                name: 'ref_date'
            }, {
                name: 'driver'
            }, {
                name: 'card_code'
            }, {
                name: 'ref_price'
            }, {
                name: 'ref_area'
            }, {
                name: 'yard'
            }, {
                name: 'remark'
            }]
    });

    var store = new Ext.data.Store({
        storeId: 'store1',
        proxy: new Ext.data.HttpProxy({
            url: 'RefuelCardJson.action'
        }),
        reader: jReader1,
        baseParams: {
        }
    });

    var sm = new Ext.grid.CheckboxSelectionModel({
    });

    var cm = new Ext.grid.ColumnModel([sm, {
            header: '编号',
            sortable: true,
            hidden: true,
            dataIndex: 'id'
        }, {
            header: '车牌号码',
            sortable: true,
            width: 50,
            dataIndex: 'car_code'
        }, {
            header: '油卡编号',
            sortable: true,
            width: 50,
            dataIndex: 'card_code'
        }, {
            header: '备注',
            sortable: true,
            dataIndex: 'remark'
        }]);

    var grid = new Ext.grid.GridPanel({
        id: 'refuel_grid',
        store: store,
        anchor: '100% -40',
        viewConfig: {
            forceFit: true
        },
        sm: sm,
        listeners: {
        },
        cm: cm,
        frame: true,
        columnLines: true,
        autoWidth: true,
        loadMask: true, // 数据加载时的遮罩功能
        stateful: true,
        stateId: 'grid'
//        pageSize: 20,
//        bbar: new Ext.PagingToolbar({
//            store: store,
//            emptyMsg: '无数据',
//            displayInfo: true
//        })
    });
    store.load();

    var viewport = new Ext.Viewport({
        layout: 'border',
        items: [{
                region: 'center',
                layout: 'border',
                items: [{
                        tbar: toolbar,
                        region: 'center',
                        // layout : 'accordion',
                        layout: "anchor",
                        frame: true,
                        deferredRender: false,
                        activeTab: 0,
                        items: [form, grid]
                    }]
            }]
    });

    function del() {
        var del_data = Ext.ComponentMgr.get("refuel_grid")
                .getSelectionModel().getSelections();
        if (del_data.length == 0) {
            Ext.Msg.alert('提示', '请选择需要删除的加油数据！');
            return;
        } else {
            var id_array = [];
            for (var i = 0; i < del_data.length; i++) {
                id_array[i] = del_data[i].data.id;
            }
            sendRequest(id_array);
        }
    }

    function edit() {
        var edit_data = Ext.ComponentMgr.get("refuel_grid")
                .getSelectionModel().getSelections();
        if (edit_data.length == 1) {
            card.getForm().loadRecord(edit_data[0]);
            card_win.show();
        }
    }

    function sendRequest(data) {
        Ext.Ajax.request({
            url: 'DeleteAction.action',
            params: {
                id: data,
                type: 'refuelCard'
            },
            success: function(response, options) {
                Ext.Msg.alert('提示', '删除成功！');
                store.reload();
            },
            failure: function(response, options) {
                Ext.Msg.alert('提示', '删除失败！');
            }
        });
    }


    var store_refuel = new Ext.data.Store({
        storeId: 'store1',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=refuel'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                }, {
                    name: 'value'
                }
            ]
        })
    });

    var card = new Ext.FormPanel({
        id: 'card',
        name: 'card',
        labelWidth: 80,
        width: '100%',
        height: '100%',
        bodyStyle: 'padding: 4px',
        border: false,
        autoscroll: true,
        frame: true,
        autoScroll: true,
        items: [{
                layout: 'column',
                items: [{
                        name: 'id',
                        xtype: 'hidden'
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                                xtype: "textfield",
                                fieldLabel: "油卡编号",
                                emptyText: '油卡编号(必填)',
                                disabled: false,
                                allowBlank: false,
                                id: 'card_code',
                                name: 'card_code',
                                anchor: "95%"
                            }]
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                                xtype: 'combo',
                                fieldLabel: '车牌号码',
                                name: 'car_code',
                                displayField: 'name',
                                valueField: 'name',
                                emptyText: '车牌号码',
                                anchor: '95%',
                                typeAhead: true,
                                queryParam: 'name',
                                autoSelect: false,
                                        triggerAction : 'all',
                                        store: carList_store
                            }]
                    }]
            }, {
                xtype: "htmleditor",
                fieldLabel: 'Biography',
                fieldLabel : "备注",
                        emptyText: '备注',
                disabled: false,
                allowBlank: false,
                id: 'remark',
                name: 'remark',
                anchor: "98%"
            }]
    });

    var card_win = new Ext.Window({
        id: 'card_win',
        title: "添加油卡记录",
        border: false,
        bodyBorder: false,
        // layout:'fit',
        width: 630,
        closeAction: 'hide',
        modal: true,
        tbar: ['-', {
                text: ' 提交并继续',
                handler: function() {
                    onSubmit();
                }
            }, '-', {
                text: ' 提交并返回',
                handler: function() {
                    onSubmit();
                    card_win.hide();
                }
            }, '-', {
                text: ' 返回',
                handler: function() {
                    card_win.hide();
                    card.getForm().reset();
                }
            }, '-'],
        items: [card]
    });


    function onSubmit() {
        // 提交表单
        if (card.getForm().isValid()) {
            var submitValues = card.getForm().getValues();
            for (var param in submitValues) {
                if (card.form.findField(param)
                        && card.form.findField(param).emptyText == submitValues[param]) {
                    card.form.findField(param).setValue(' ');
                }
            }
            card.getForm().submit({
                url: 'CardAction.action',
                waitMsg: '数据保存中...',
                submitEmptyText: false,
                success: function(c, d) {
                    Ext.MessageBox.alert("操作信息", "成功信息保存！");
                    card.getForm().reset();
                    store.reload();
                },
                failure: function(c, d) {
                    Ext.MessageBox.show({
                        title: "操作信息",
                        msg: "信息保存出错，请联系管理员！",
                        buttons: Ext.MessageBox.OK,
                        icon: "ext-mb-error"
                    });

                }
            });

        }

    }


});